﻿using DataLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Common;

namespace ImageUploader
{
    public partial class Form_Login : Form
    {        
        public Form_Login()
        {
            InitializeComponent();
        }
        private ImageManagerEntities ime = new ImageManagerEntities();        
        private void btn_Login_Click(object sender, EventArgs e)
        {
            try
            {
                int c = ime.Users.Count();
                if (c < 1)
                {
                    MessageBox.Show("尚无任何操作员");
                }
                else
                {
                    PublicMethod pm = new PublicMethod();
                    var password = pm.EncryptString(txt_Password.Text);
                    var users = ime.Users.Where(u => u.UserNum == (string)cbox_User.SelectedValue && u.Password == password).ToList();
                    if (users.Count() == 1)
                    {                        
                        var user = users.First();
                        string ip = pm.GetLocalIP();
                        if (RedisHelper.Get<string>(user.UserID.ToString()) != null && user.Logined_IP != ip)
                        {                            
                            MessageBox.Show("您已在其他机器登录，如果是异常退出造成的无法登录，请及时联系管理员或者等待2小时后再重新登录!");
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                            GetUserInfo.UserID = user.UserID;
                            GetUserInfo.Location = string.IsNullOrEmpty(user.Location) ? "保定" : user.Location;
                            GetUserInfo.UserNum = user.UserNum;
                            GetUserInfo.RoleID = user.RoleID;
                            user.Logined_at = System.DateTime.Now;
                            user.Logined_IP = ip;
                            ime.SaveChanges();
                            RedisHelper.Set<string>(user.UserID.ToString(), user.UserNum, DateTime.Now.AddHours(2) - DateTime.Now);
                            this.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误");
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Press_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btn_Login_Click(sender, e);//触发button事件  
            }
        }
        private void Login_Load(object sender, EventArgs e)
        {
            try
            {
                var users = ime.Users;
                if(users.Count() == 0)
                {
                    MessageBox.Show("尚无用户数据");
                    return;
                }
                List<string> list = new List<string>();

                foreach (var user in users)
                {
                    list.Add(user.UserNum);
                }
                cbox_User.DataSource = list;
                var source = new AutoCompleteStringCollection();
                foreach (var id in list)
                {
                    source.Add(id);
                }
                cbox_User.AutoCompleteCustomSource = source;
                cbox_User.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                cbox_User.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }
            catch
            {
                MessageBox.Show("服务器连接失败，请确认网络通畅");
            }
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            if(GetUserInfo.UserID > 0)
                RedisHelper.DelKey(GetUserInfo.UserID.ToString());
            Application.Exit();
        }

    }
}
